package cn.cheegon.blog.service.impl;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.entity.dto.PmsTypeDto;
import cn.cheegon.blog.mapper.CmsBlogMapper;
import cn.cheegon.blog.mapper.CmsBlogTagsMapper;
import cn.cheegon.blog.mapper.PmsTypeMapper;
import cn.cheegon.blog.model.CmsBlog;
import cn.cheegon.blog.model.PmsType;
import cn.cheegon.blog.service.PmsTypeService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 类型表 服务实现类
 * </p>
 *
 * @author cheegon
 * @since 2024-06-12
 */
@Slf4j
@Service
public class PmsTypeServiceImpl extends ServiceImpl<PmsTypeMapper, PmsType> implements PmsTypeService {

    @Autowired
    PmsTypeMapper typeMapper;

    @Autowired
    CmsBlogMapper blogMapper;


    @Override
    public PmsTypeDto getItemById(Long id) {
        return BeanUtil.toBean(this.baseMapper.selectById(id), PmsTypeDto.class);
    }

    @Override
    public CommonPage<PmsTypeDto> getList(int pageNum, int pageSize) {
        QueryWrapper<PmsType> queryWrapper = new QueryWrapper<>();
        Page<PmsType> pageInfo = typeMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        if (pageInfo.getTotal() == 0) {
            return CommonPage.emptyPage(pageInfo);
        }
        List<PmsTypeDto> collect = pageInfo.getRecords().stream().map(
                it -> {
                    PmsTypeDto dto = new PmsTypeDto();
                    BeanUtils.copyProperties(it, dto);
                    return dto;
                }
        ).collect(Collectors.toList());
        return CommonPage.restPage(pageInfo, collect);
    }

    public List<PmsTypeDto> getTopList(int size) {
        List<PmsTypeDto> topList = typeMapper.getTopList(size);
        // log.debug("topList:{}", JSON.toJSONString(topList));
        if (CollectionUtils.isEmpty(topList)) {
            return Collections.emptyList();
        }
        return topList;
    }

    @Override
    public List<PmsTypeDto> getAllList() {
        QueryWrapper<PmsType> queryWrapper = new QueryWrapper<>();
        List<PmsType> pmsTags = typeMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(pmsTags)) {
            return Collections.emptyList();
        }
        return pmsTags.stream().map(
                it -> {
                    PmsTypeDto dto = new PmsTypeDto();
                    BeanUtils.copyProperties(it, dto);
                    return dto;
                }
        ).collect(Collectors.toList());
    }

    @Override
    public void add(PmsType dto) {
        typeMapper.insert(dto);
    }

    @Override
    public void edit(PmsType dto) {
        typeMapper.updateById(dto);
    }

    @Override
    public void del(Long id) {
        typeMapper.deleteById(id);
        List<CmsBlog> cmsBlogs = blogMapper.selectList(new QueryWrapper<>(new CmsBlog()).eq("type_id", id));
        if (!CollectionUtils.isEmpty(cmsBlogs)) {
            cmsBlogs.forEach(it -> {
                UpdateWrapper<CmsBlog> wrapper = new UpdateWrapper<>();
                wrapper.eq("id", it.getId());
                wrapper.set("type_id", null);
                blogMapper.update(wrapper);
            });
        }
    }
}
